package com.thinkgem.jeesite.modules.quartz.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.quartz.CronScheduleBuilder;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.quartz.entity.Schedulejob;
import com.thinkgem.jeesite.modules.quartz.service.SchedulejobService;




/**
 * 任务调度模块Controller
 * @author zhangwei
 * @version 2015-05-20
 */
@Controller
@RequestMapping(value = "${adminPath}/quartz/scheduleJob")
public class SchedulejobController extends BaseController {
	public final Logger log = Logger.getLogger(this.getClass());
	@Autowired
	private SchedulejobService scheduleJobService;
	
	@ModelAttribute
	public Schedulejob get(@RequestParam(required=false) String id) {
		Schedulejob entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = scheduleJobService.get(id);
		}
		if (entity == null){
			entity = new Schedulejob();
		}
		return entity;
	}
	
	@RequiresPermissions("quartz:scheduleJob:view")
	@RequestMapping(value = {"list", ""})
	public String list(Schedulejob quaSchedulejob, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Schedulejob> page = scheduleJobService.findPage(new Page<Schedulejob>(request, response), quaSchedulejob); 
		model.addAttribute("page", page);
		return "modules/quartz/schedulejobList";
	}

	@RequiresPermissions("quartz:scheduleJob:view")
	@RequestMapping(value = "form")
	public String form(Schedulejob quaSchedulejob, Model model) {
		model.addAttribute("quaSchedulejob", quaSchedulejob);
		return "modules/quartz/schedulejobForm";
	}

	@RequiresPermissions("quartz:scheduleJob:edit")
	@RequestMapping(value = "save")
	public String save(Schedulejob quaSchedulejob, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, quaSchedulejob)){
			return form(quaSchedulejob, model);
		}
		scheduleJobService.save(quaSchedulejob);
		addMessage(redirectAttributes, "保存任务调度成功");
		return "redirect:"+Global.getAdminPath()+"/quartz/scheduleJob/?repage";
	}
	
	@RequiresPermissions("quartz:scheduleJob:edit")
	@RequestMapping(value ="changeJobStatus")
	public String changeJobStatus(HttpServletRequest request, String id, String cmd,RedirectAttributes redirectAttributes) {
		try {
			scheduleJobService.changeStatus(id, cmd);
			addMessage(redirectAttributes, cmd+"成功");
		} catch (SchedulerException e) {
			log.error(e.getMessage(), e);
			addMessage(redirectAttributes, cmd+"启动失败");
		}
		return "redirect:"+Global.getAdminPath()+"/quartz/scheduleJob/?repage";
	}
	@RequiresPermissions("quartz:scheduleJob:edit")
	@RequestMapping("updateCron")
	public @ResponseBody Map<String, Object> updateCron(String id, String cron) {
		String result="cron更新成功！";
		Map<String, Object> map = new HashMap<String, Object>(1);
		try {
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
			scheduleJobService.updateCron(id, cron);
			map.put("success", true);
		} catch (SchedulerException e) {
			result="cron表达式有误，不能被解析！";
			map.put("success", false);
		} catch (Exception e) {
			result="cron表达式有误，不能被解析！";
			map.put("success", false);
		}
	    map.put("result", result);
	    return map;
	}
	
	
	@RequiresPermissions("quartz:scheduleJob:edit")
	@RequestMapping(value = "delete")
	public String delete(Schedulejob quaSchedulejob, RedirectAttributes redirectAttributes) {
		scheduleJobService.delete(quaSchedulejob);
		addMessage(redirectAttributes, "删除任务调度成功");
		return "redirect:"+Global.getAdminPath()+"/quartz/scheduleJob/?repage";
	}

}